home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / LDFLOOR.C < prev    next >
Text File  |  1993-04-05  |  2KB  |  42 lines

  1. /* ldfloor() -- long double floor
  2. ** public domain by Raymond Gardner  Englewood, CO
  3. ** tested with TC++
  4. ** assumptions: 80-bit IEEE format numbers, stored LSB first 
  5. **   (Intel style), longs & ints are accessed from arbitrary boundaries
  6. */
  7.  
  8. long double ldfloor(long double a)
  9. {
  10.     long double a0;
  11.     int e, n;
  12.    
  13.     a0 = a;
  14.     e = ((int *)&a)[4] & 0x7FFF;        /* extract exponent         */
  15.     if ( e == 0 )                       /* 0 is special case        */
  16.         return (long double) 0.0;
  17.     e -= 16383;                         /* unbias exponent          */
  18.     if (e < 0)                          /* if < 0, num is < 1,...   */
  19.     {
  20.         a = 0.0;                        /* so floor is zero         */
  21.     }
  22.     else if ((n = 63 - e) > 0)          /* clear n least sig. bits  */
  23.     {
  24.         if (n < 32)                     /* clear n lowest bits      */
  25.         {
  26.             ((unsigned long *)&a)[0] &= ~((1L << n) - 1);
  27.         }
  28.         else                            /* n >= 32 */
  29.         {
  30.             ((unsigned long *)&a)[0] = 0; /* clear lower 32 bits            */
  31.             n -= 32;                    /* how many left to clear ?         */
  32.             if (n)                      /* if any, clear n next lowest bits */
  33.             {
  34.                 ((unsigned long *)&a)[1] &= ~((1L << n) - 1);
  35.             }
  36.         }
  37.     }
  38.     if (a0 < 0 && a0 != a)          /* if neg. and it had fractional bits */
  39.         a -= 1.0;                   /* adjust the floor                   */
  40.     return a;                       /* return it                          */
  41. }
  42.